/*
 * @Descripttion: 本地存储 Storage
 * @Define: 定义
 * @Use: 使用
 * @Author: yeming
 * @Date: 2018-06-05 15:51:42
 */

const Storage = {};

// localStorage 存值
Storage.setLocal = function (key, value) {
  if (typeof value === "string") {
    localStorage.setItem(key, value);
  } else {
    localStorage.setItem(key, JSON.stringify(value));
  }
};

// localStorage 取值
Storage.getLocal = function (key) {
  const str = localStorage.getItem(key);
  if (typeof str === "string") {
    try {
      return JSON.parse(str);
    } catch (e) {
      return str;
    }
  }
  // 不存在当前 key 则返回 false，判断当前 localStorage 是否存在
  return false;
};

// localStorage 删除指定项
Storage.delLocal = function (key) {
  localStorage.removeItem(key);
};

// localStorage 清除所有项
Storage.clearLocal = function () {
  localStorage.clear();
};

// sessionStorage 存值
Storage.setSession = function (key, value) {
  if (typeof value === "string") {
    sessionStorage.setItem(key, value);
  } else {
    sessionStorage.setItem(key, JSON.stringify(value));
  }
};

// sessionStorage 取值
Storage.getSession = function (key) {
  const str = sessionStorage.getItem(key);
  if (typeof str === "string") {
    try {
      return JSON.parse(str);
    } catch (e) {
      return str;
    }
  }
  // 不存在当前 key 则返回 false，判断当前 localStorage 是否存在
  return false;
};

// sessionStorage 删除指定项
Storage.delSession = function (key) {
  sessionStorage.removeItem(key);
};

// sessionStorage 清除所有项
Storage.clearSession = function () {
  sessionStorage.clear();
};

export default Storage;
